﻿using System;
using System.Collections.Generic;
using System.Net;
using System.ServiceModel;
using System.Windows;
using System.Windows.Threading;

using SBPweb.RIA.Client.Common.Collections;
using SBPweb.RIA.Client.Common.Patterns;

using SBPweb.PlAzureNet.Shared.Library.Actors;

using SBPweb.PlAzureNet.Client.Library.Services.Data;

namespace SBPweb.PlAzureNet.Client.Library.Controllers
{

    public class DataServiceController
    {
        
        #region Fields

        private static SingletonHelper<DataServiceController> sh = new SingletonHelper<DataServiceController>();

        //private CookieContainer cookies = new CookieContainer();
        private DataServiceContractClient dataService;

        #endregion

        #region Constructor logic

        public DataServiceController()
        {
            dataService = new DataServiceContractClient();
            //dataService.CookieContainer = cookies;

            Dispatcher = Application.Current.RootVisual.Dispatcher;

            Pleasures = new ManagableReadOnlyObservableList<IPleasure>();

            dataService.FetchAllPleasuresCompleted += new EventHandler<FetchAllPleasuresCompletedEventArgs>(dataService_FetchAllPleasuresCompleted);
            dataService.FetchOnePersonByIdCompleted += new EventHandler<FetchOnePersonByIdCompletedEventArgs>(dataService_FetchOnePersonByIdCompleted);
        }

        #endregion

        #region Properties

        public static DataServiceController Instance
        {
            get
            {
                return sh.Instance;
            }
        }

        internal Dispatcher Dispatcher
        {
            get;
            private set;
        }


        public IPerson Person
        {
            get;
            private set;
        }

        public ManagableReadOnlyObservableList<IPleasure> Pleasures
        {
            get;
            private set;
        }

        #endregion

        #region Methods

        public void FetchAllPleasures()
        {
            dataService.CallInContext<IDataServiceContract, object>(dataService.FetchAllPleasuresAsync, this);
            
            //dataService.FetchAllPleasuresAsync(this);
        }

        public void FetchOnePersonByIds(int providerId, string userId)
        {
            dataService.FetchOnePersonByIdAsync(providerId, userId, this);
            //dataService.CallInContext<IDataServiceContract, int, long, object>(dataService.FetchOnePersonByIdAsync, providerId, userId, this);
        }

        #endregion

        #region Event handlers

        private void dataService_FetchAllPleasuresCompleted(object sender, FetchAllPleasuresCompletedEventArgs e)
        {
            if (e.UserState != this)
            {
                return;
            }
            if (e.Error != null)
            {
                throw e.Error;
            }

            Pleasures.ManagableItems.Clear();
            foreach (var p in e.Result)
            {
                Pleasures.ManagableItems.Add(p);
            }
        }

        private void dataService_FetchOnePersonByIdCompleted(object sender, FetchOnePersonByIdCompletedEventArgs e)
        {
            Person = e.Result;
            Dispatcher.BeginInvoke(() =>
            {
                TrackingServiceController.Instance.Action.PersonId = Person.Id;
            });
        }

        #endregion

    }

}